package com.hspedu.newData.dmsxl.树;

import com.hspedu.newData.dmsxl.ds.TreeNode;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-05-09 
 * @Version: 1.0
 */

public class LC543二叉树的直径 {

    int ans = 0;
    public int diameterOfBinaryTree(TreeNode root) {

        ans = 1;
        depth(root);
        return ans - 1;
    }


    public int depth(TreeNode root){
        if (root == null){
            return 0;
        }
        int left = depth(root.left);
        int right = depth(root.right);

        ans = Math.max(ans,left+right+1);

        return Math.max(left,right) + 1;
    }
}
